package test11;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Award implements Runnable {
    private static int[] arr = {10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700};
    private static Lock lock = new ReentrantLock();
    private static ArrayList<Integer> list = new ArrayList<>();
    @Override
    public /*synchronized*/ void run() {
        //创建集合：


        //随机取出数组的下标到集合中的；
        Random random = new Random();
        while (list.size() != arr.length) {
            int i = random.nextInt(arr.length);
            if (!list.contains(i)) {//list集合中保存着散列的下标：
                list.add(i);
            }
        }

        synchronized (this) {
            while (list.size()!=0){
                int i =0;
                System.out.println(Thread.currentThread().getName() + "产生了一个" + arr[list.remove(i)] + "元大奖");
            }
        }
    }
        /*lock.lock();

        for (int i1 = 0; i1 < arr.length; i1++) {
            System.out.println(Thread.currentThread().getName() + "产生了一个" + arr[list.get(i1)] + "元大奖");
        }

        lock.unlock();
    }*/
}
